﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var SWAREHOUSEID = $('#SWAREHOUSEID').combobox('getValue');
    var SLOCATIONCODE = $.trim($("#txtSLOCATIONCODE").attr("value"));
    var SLOCATIONTYPE = $('#SLocationType').combobox('getValue');
    var SArea = $.trim($("#txtSArea").attr("value"));
    var SRoadWay = $.trim($("#txtSRoadWay").attr("value"));
    var SSTATUS = $('#SSTATUS').combobox('getValue');
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/Location/Query',
        title: '货位列表',
        queryParams: {
            SWAREHOUSEID: SWAREHOUSEID,
            SLOCATIONCODE: SLOCATIONCODE,
            SLOCATIONTYPE: SLOCATIONTYPE,
            SArea: SArea,
            SRoadWay: SRoadWay,
            SSTATUS: SSTATUS
        },
        //width: 815,
        height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'LOCATIONID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'LOCATIONID', title: '货位ID', hidden: true },
                    { field: 'LOCATIONCODE', title: '货位编码', align: 'center', sortable: true },
                    { field: 'LOCATIONTYPE', title: '货位类型', align: 'center', sortable: true },
                    { field: 'WAREHOUSENAME', title: '仓库', align: 'center', sortable: true },
                    { field: 'CELLNO', title: '库位', align: 'center', sortable: true },
                    { field: 'GOODSRACK', title: '货架', align: 'center', sortable: true },
                    { field: 'TIER', title: '层', align: 'center', sortable: true },
                    { field: 'COLUMNS', title: '列', align: 'center', sortable: true },
                    { field: 'AREA', title: '区域', align: 'center', sortable: true },
                    { field: 'ROADWAY', title: '巷道', align: 'center', sortable: true },
                    { field: 'VOLUME', title: '体积', align: 'center', sortable: true },
                    { field: 'CREATER', title: '创建人', align: 'center', sortable: true },
                    { field: 'CREATEDATE', title: '创建时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'EDITOR', title: '修改人', align: 'center', sortable: true },
                    { field: 'EDITDATE', title: '修改时间', align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'STATUS', title: '状态', align: 'center', sortable: true, formatter: formatstatus },
                    { field: 'REMARK', title: '备注', align: 'center', sortable: true }
        ]],
        toolbar: [{
            id: 'btnAdd',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }, '-', {
            id: 'btnRemove',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}
function formatstatus(val) {
    if (val != null) {
        if (val == 0) {
            return "失效";
        }
        if (val == 1) {
            return "生效";
        }
    }
}

function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }

    return "";
}

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');

    //$('#btnImport').linkbutton('disable');
    // $('#btnExport').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
        $('#btnRemove').linkbutton('enable');
    }
}


//增加
function add() {
    $('#p1').dialog('open').dialog('setTitle', '新增货位资料信息');
    $("#hiddenLOCATIONID").val("");
    $("#hiddenLocationCode").val("");
    $("#txtLocationCode").val("");
    //$("#txtLocationType").val("");
    $("#txtGOODSRACK").val("");
    $("#txtTier").val("");
    $("#txtColumns").val("");
    $("#txtArea").val("");
    $("#txtRoadWay").val("");
    $("#txtVolume").val("");
    $("#txtRemark").val("");
    $('#WAREHOUSEID').combogrid("setValue", "");
    $('#CELLNO').combogrid("setValue", "0");
    $('#LocationType').combobox("setValue", "");
    //$('#STATUS').combobox("setValue", "");
    url = '/Location/Saves';
}

//修改
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#p1').dialog('open').dialog('setTitle', '修改货位资料信息');
    $("#hiddenLOCATIONID").val(rows[0].LOCATIONID);
    $("#hiddenLocationCode").val(rows[0].LOCATIONCODE);
    $("#txtLocationCode").val(rows[0].LOCATIONCODE);
    //$("#txtLocationType").val(rows[0].LOCATIONTYPE);
    $("#txtGOODSRACK").val(rows[0].GOODSRACK);
    $("#txtTier").val(rows[0].TIER);
    $("#txtColumns").val(rows[0].COLUMNS);
    $("#txtArea").val(rows[0].AREA);
    $("#txtRoadWay").val(rows[0].ROADWAY);
    $("#txtVolume").val(rows[0].VOLUME);
    $("#txtRemark").val(rows[0].REMARK);
    $('#CellNo').combogrid("setValue", rows[0].CELLNO);
    $('#WAREHOUSEID').combogrid("setValue", rows[0].WAREHOUSEID);
    $('#LocationType').combobox("setValue", rows[0].LOCATIONTYPE);
    //$('#STATUS').combobox("setValue", rows[0].STATUS);
    url = '/Location/Saves';

}



//删除
function del() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].LOCATIONID);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            $('#location').form('submit', {
                url: '/Location/Delete' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGrid();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}


function save() {
    var LOCATIONID = $.trim($("#hiddenLOCATIONID").attr("value"));
    var LocationCode = $.trim($("#txtLocationCode").attr("value"));
    //var LocationType = $.trim($("#txtLocationType").attr("value"));
    var GOODSRACK = $.trim($("#txtGOODSRACK").attr("value"));
    var Tier = $.trim($("#txtTier").attr("value"));
    var Columns = $.trim($("#txtColumns").attr("value"));
    var Area = $.trim($("#txtArea").attr("value"));
    var RoadWay = $.trim($("#txtRoadWay").attr("value"));
    var Volume = $.trim($("#txtVolume").attr("value"));
    var wid = $('#WAREHOUSEID').combobox('getValue');
    var CellNo = $('#CellNo').combobox('getValue');
    var ltype = $('#LocationType').combobox('getValue');

    if (LocationCode == "") {
        $.messager.alert('提示', '货位编码不能为空', 'warning');
        $("#txtLocationCode").focus();
        return false;
    }
    if (ltype == null || ltype == "") {
        $.messager.alert('提示', '请选择货位类型', 'warning');
        $("#LocationType").focus();
        return false;
    }
    else {
        if (ltype == "存储货位") {
            if (CellNo == "") {
                $.messager.alert('提示', '库位不能为空', 'warning');
                $("#CellNo").focus();
                return false;
            }
        }
    }
    if (wid == null || wid == "") {
        $.messager.alert('提示', '请选择仓库', 'warning');
        $("#WAREHOUSEID").focus();
        return false;
    }


    $('#location').form('submit', {
        url: url+ '?LOCATIONID=' + LOCATIONID,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p1').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}


//提交参数
function submitParams() {
    $('#fm2').form('submit', {
        url: '/Distr/UpdateParams',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                grid.datagrid('reload');
                $.messager.alert('提示', '参数修改成功!', 'info');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

